//
//  NSObject+swizzle.m
//  Test
//
//  Created by xueshan1 on 2019/6/5.
//  Copyright © 2019 xueshan1. All rights reserved.
//

#import "NSObject+swizzle.h"
#import <objc/runtime.h>
@implementation NSObject (swizzle)

- (void)swizzleInstanceMethod:(SEL)originSelector newSelector:(SEL)newSelector class:(Class)cls{
    Method originMethod = class_getInstanceMethod(cls, originSelector);
    Method newMethod    = class_getInstanceMethod(cls, newSelector);
    BOOL didAddMethod = class_addMethod(cls, originSelector, method_getImplementation(newMethod), method_getTypeEncoding(newMethod));
    if (didAddMethod) {
        class_replaceMethod(cls, newSelector, method_getImplementation(originMethod), method_getTypeEncoding(originMethod));
    }else{
        method_exchangeImplementations(originMethod, newMethod);
    }
}

+ (void)swizzleClassMethod:(SEL)originSelector newSelector:(SEL)newSelector class:(Class)cls{
    Method originMethod = class_getClassMethod(cls, originSelector);
    Method newMethod    = class_getClassMethod(cls, newSelector);
    cls = object_getClass(cls);
    BOOL didAddMethod = class_addMethod(cls, originSelector, method_getImplementation(originMethod), method_getTypeEncoding(originMethod));
    if (didAddMethod) {
        class_replaceMethod(cls, newSelector, method_getImplementation(newMethod), method_getTypeEncoding(newMethod));
    }else{
        method_exchangeImplementations(originMethod, newMethod);
    }
    
}


@end
